/// <reference path="json2.js" />
/// <reference path="jquery-1.4.2.js" />
if (!("WS" in window)) window.WS = {};
WS.sNA = "N/A";
WS.sNULL = "null";
WS.$ = function(id) { return typeof (id) === "string" ? document.getElementById(id) : id; };

WS.isVisible = function(id) {
    var e = this.$(id); if (!e || !("style" in e) || !("display" in e.style)) return false;
    var disp = (e.style.display + "").toLowerCase();
    return disp !== "none";
};

WS.toggleVisible = function(id, visible) {
    if (!id) return; if (typeof (id) !== "object" || !("length" in id)) id = [id];
    for (var i = 0, len = id.length; i < len; i++) {
        var e = this.$(id[i]); if (!e || !("style" in e) || !("display" in e.style)) continue;

        var disp = "";
        if (typeof (visible) === "undefined")
            disp = this.isVisible(e) ? "none" : "";
        if (typeof (visible) === "boolean")
            disp = visible ? "" : "none";
        else if (typeof (visible) === "string")
            disp = visible;

        e.style.display = disp;
    }
};

WS.__onErrorGetErrorDetail = function(xhr) {
    if (xhr && ("responseText" in xhr) && xhr.responseText)
        return xhr.responseText;

    if (xhr && ("responseXml" in xhr) && xhr.responseXml)
        return xhr.responseXml;
};

WS.__onSuccessGetHtml = function(data) {
    if (typeof (data) === "string") return data;
    return (data.d ? data.d : data) + "";
};

WS.__callService = function(url, params, fnBeforeSend, fnOnSuccess, fnOnError) {
    if (typeof (url) !== "string" || !url || typeof (params) === "undefined") return;
    try {
        $.ajax({
            type: "POST", //POST
            url: url, //Set call to WS Method
            data: params, // Set Method Params
            beforeSend: function(xhr) {
                xhr.setRequestHeader("Content-type", "application/json; charset=utf-8");
                if (fnBeforeSend) fnBeforeSend(xhr);
            },
            contentType: "application/json; charset=utf-8", //Set Content-Type
            dataType: "json", // Set return Data Type
            success: fnOnSuccess,
            error: fnOnError
        });
    } catch (e) { alert(e) }
};

WS.callService = function(oCaller) {
    if (!("url" in oCaller)) { alert("Please specify the url!"); return; }
    if (!("params" in oCaller)) oCaller.params = "";
    if (!("fnBeforeSend" in oCaller)) oCaller.fnBeforeSend = null;
    if (!("fnOnSuccess" in oCaller)) oCaller.fnOnSuccess = null;
    if (!("fnOnError" in oCaller)) oCaller.fnOnError = null;

    oCaller.execTime = new Date();
    this.__callService(oCaller.url, oCaller.params, oCaller.fnBeforeSend, oCaller.fnOnSuccess, oCaller.fnOnError);
};

WS.jsonToHtml = function(oJSON) {
    if (typeof oJSON === "number") return oJSON.toString();
    if (typeof oJSON === "string") return '"' + oJSON + '"';
    if (typeof oJSON === "boolean") return '' + oJSON;
    if (typeof oJSON === "function") return 'function';

    if (!oJSON) return this.sNULL;

    if ("length" in oJSON) {
        if (0 == oJSON) return "[]";
        var s = "<ol>";
        $.each(oJSON, function() {
            s += "<li>";
            s += WS.jsonToHtml(this);
            s += "</li>";
        });
        s += "</ol>";
        return s;
    }

    var ss = "<ul>";
    for (var key in oJSON) {
        ss += "<li>" + unescape(key) + "(" + typeof (oJSON[key]) + "): ";
        ss += WS.jsonToHtml(oJSON[key]);
        ss += "</li>";
    }
    ss += "</ul>";
    return ss;
};

WS.createNewCaller = function(url, params, fnBeforeSend, fnCallback, fnOnError) {
    if (typeof params !== "string") params = JSON.stringify(params);
    var caller = {
        url: url,
        params: params,
        execTime: null,
        oResponse: { sText: "", oJSON: null, sHtml: "" },
        fnBeforeSend: fnBeforeSend,
        fnOnSuccess: function(data) {
            caller.oResponse.sText = WS.__onSuccessGetHtml(data);
            caller.oResponse.oJSON = null;
            try { caller.oResponse.oJSON = JSON.parse(caller.oResponse.sText); } catch (e) { }
            if (fnCallback) fnCallback(caller);
        },
        fnOnError: function(xhr) {
            var errorDetail = WS.__onErrorGetErrorDetail(xhr) || "[Unknown Error]";
            if (fnOnError) { fnOnError(errorDetail); return; }
            alert(errorDetail);
            document.write("<pre>" + errorDetail + "</pre>");
        },
        toHtml: function() {
            var html = 'Url: <pre>';
            html += unescape(this.url) + '</pre><br />';
            html += 'Params: <pre>';
            var oParams = JSON.parse(this.params);
            html += WS.jsonToHtml(oParams) + '</pre><br />';
            html += 'Exec Timestamp: <pre>';
            html += unescape(this.execTime) + '</pre><br />';
            html += 'Result: <pre>';
            html += WS.jsonToHtml(this.oResponse.oJSON) + "</pre><br />";
            html += 'Response Text: <div class="pre">'
            html += unescape(this.oResponse.sText) + '</pre>';
            return html;
        }
    };

    return caller;
};